<?php


include_once 'plan_func.php';
include_once 'functions.php';
session_start();

if (! isset ( $_SESSION ['username'] )) {
	header ( 'Location: index.php' );
}



function planMatches($emptyPlan, $newPlan){
    $i = 0;
    $len = strlen($emptyPlan);
    
    if($len != strlen($newPlan)) return false;
    
    $semiLoc = strpos($emptyPlan, ';');
    $semiLoc = strpos($emptyPlan, ';', $semiLoc + 1);
    
    if(substr($emptyPlan, 0, $semiLoc + 1) != substr($newPlan, 0, $semiLoc + 1)) return false;
    $i = $semiLoc + 1;
    while($i < $len){
        $newSub = substr($newPlan, $i, 2);
        if((substr($emptyPlan, $i, 2) == '**') != ($newSub == '**')) return false;
        if(!is_numeric($newSub)){
            if($newSub != '**' && $newSub != '__'){
                return false;
            }
        }
        
        $i += 2;
    }
    return true;
}


$allBlocks = getAllBlocks();

if(isset($_POST['overwrite']) && $_POST['overwrite'] == 1){
    $overwrite = 1;
}else{
    $overwrite = 0;
}

if(isset($_POST['new']) && $_POST['new'] == 1){
    $new = 1;
    $saveOrCreate = "Create";
}else{
    $new = 0;
    $saveOrCreate = "Save";
}

if(isset($_POST['blocktype']) && $_POST['blocktype'] == 1){
    $blocktype = 1;
}else{
    $blocktype = 0;
}

if(isset($_POST['blockid']) && $_POST['blockid']){
    $firstBlock = null;
    foreach($allBlocks as $b){
        if($b['blockid'] == $_POST['blockid']){
            $firstBlock = $b;
            break;
        }
    }
    if($firstBlock){
        $emptyMap = getEmptyMap($firstBlock['blockid']);
        if(isset($_POST['name']) && $_POST['name']){
            $name = $_POST['name'];
            if(strlen($name) <= 100){
                if(stripslashes($name) == $name){
                    if($new == 1){
                        $name = date("Y-m-d") . ": $name";
                        addPlan($firstBlock['blockid'], $blocktype, $name, $emptyMap);
                        echo $name;
                    }elseif(isset($_POST['plan']) && $_POST['plan']){
                        if(planMatches($emptyMap, $_POST['plan'])){
                            if($overwrite == 0){
                                $name = date("Y-m-d") . ": $name";
                                addPlan($firstBlock['blockid'], $blocktype, $name, $_POST['plan']);
                                echo $name;
                            }else{
                                updatePlan($firstBlock['blockid'], $blocktype, $name, $_POST['plan']);
                                echo $name;
                            }
                        }else{
                            echo "Save failed. Your plan doesn't match up with the map of that block.";
                        }
                    }else{
                        echo "Save failed. No plan string specified.";
                    }
                }else{
                    echo "$saveOrCreate failed. Supplied name should only have alpha-numeric characters.";
                }
            }else{
                echo "$saveOrCreate failed. Supplied name was too long (Max allowed is 100 letters).";
            }
        }else{
            echo "$saveOrCreate failed. No name given.";
        }
    }else{
        echo "$saveOrCreate failed. Unrecognized block id.";
    }
}else{
    echo "$saveOrCreate failed. No block id specified.";
}


?>